package br.edu.curso.java.dao;


import static br.edu.curso.java.dao.BaseDAO.getConexao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import br.edu.curso.java.VO.AlunosDoProfessorVO;
import br.edu.curso.java.VO.ProfessorVO;


public class AlunosDoProfessorDAO extends BaseDAOChaveUnica<AlunosDoProfessorVO, Integer> {
    
    @Override
	public String getNomeTabela() {
		// TODO Auto-generated method stub
		return "alunosdoprofessor";
	}

	@Override
	protected AlunosDoProfessorVO getObjeto() {
		// TODO Auto-generated method stub
		return new AlunosDoProfessorVO();
	}

	@Override
	protected PreparedStatement carregarChavePrimaria(PreparedStatement pstm,
			Integer codigo) {
		
		try {
			
			pstm.setInt(1, codigo);
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return pstm;
	}
	
	
	@Override
	public String getChavePrimaria() {
		
		return "nsu";
	}

	
	@Override
	protected int carregarCampos(PreparedStatement pstm,
				AlunosDoProfessorVO obj, int i) {
		
		try {
			
			i = 0;
			
			pstm.setInt(++i, obj.getCodigoAluno());
			pstm.setInt(++i, obj.getCodigoProfessor());
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return i;
	}

	@Override
	protected int carregarChavePrimaria(PreparedStatement pstm,
			AlunosDoProfessorVO obj, int i) {

		try {
			pstm.setInt(++i, obj.getNsu());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return i;
	}

	@Override
	protected AlunosDoProfessorVO carregarObjeto(ResultSet rs) {
		
		AlunosDoProfessorVO  obj = new AlunosDoProfessorVO();
		
		try {
			
			obj.setNsu(rs.getInt("nsu"));
			obj.setCodigoAluno(rs.getInt("codigo_aluno"));
			obj.setCodigoProfessor(rs.getInt("codigop_rofessor"));
									
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

		return obj;
	}
        
         public  void excluirProfessoresDoAluno(Integer codigoAluno) {
			
			try {
				Connection conexao = getConexao();
				Statement stm = conexao.createStatement();
				
                                PreparedStatement pstm =  conexao.prepareCall("delete from alunosdoprofessor "
							   	+ "where alunosdoprofessor.codigo_aluno = ?;");
                                        
                                pstm.setInt(1,  codigoAluno);
			
                                pstm.execute();
				stm.close();
				conexao.close();
								
			} catch (Exception e) {
				e.printStackTrace();
			}
		
					}   
	
	 protected Integer carregarChave(ResultSet rs) {
                
            Integer i = 0;
            
            try {
                
                 i = rs.getInt(1);
             
            } catch (SQLException e) {
		e.printStackTrace();
            }
            
            return i;
            
          };
 
}